/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package consys.social.core.service;

import consys.social.core.BasicTranscoder;
import consys.social.core.bo.DiscussionPost;
import consys.social.core.bo.Entity;
import java.text.ParseException;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author petan
 */
public class DiscussionPostTranscoder implements BasicTranscoder<DiscussionPost> {

    private Logger logger = Logger.getLogger(DiscussionPostTranscoder.class);
    
    @Override
    public String encodeToString(DiscussionPost object) {
        return object.toString();
    }

    @Override
    public DiscussionPost decodeFromString(String data) {
        String[] parts = StringUtils.split(data, ":", 3);
        DiscussionPost dp = new DiscussionPost();
        try {
            dp.setDate(SocialUtils.stringToDate(parts[0]));
        } catch (ParseException ex) {
            logger.error("Date in unexpected format");
            throw new IllegalArgumentException(ex);
        }
        dp.setAddedBy(new Entity(parts[1]));
        dp.setText(parts[2]);
        return dp;
    }

}
